import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Res } from '@nestjs/common';
import { ChatService } from './chat.service';
import { CreateMessageDto, CreateSessionDto } from './dto/chat.dto';
import { ApiBearerAuth, ApiTags, ApiOperation } from '@nestjs/swagger';
import { JwtGuard } from 'src/common/guards/jwt.guard';
import { Response } from 'express';

@ApiTags('聊天模块')
@ApiBearerAuth()
@UseGuards(JwtGuard)
@Controller('chat')
export class ChatController {
  constructor(private readonly chatService: ChatService) {}

  @Post('create')
  @ApiOperation({ summary: '对话接口' })
  create(@Body() body: CreateMessageDto, @Res() res: Response) {
    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');
    res.flushHeaders(); //立即发送响应头
    return this.chatService.create(body, res);
  }

  @Post('createSession')
  @ApiOperation({ summary: '创建会话' })
  createSession(@Body() body: CreateSessionDto) {
    return this.chatService.createSession(body);
  }

  @Get('getSessions/:id')
  @ApiOperation({ summary: '根据当前人id查询所有会话' })
  getAllSessions(@Param('id') id: string) {
    return this.chatService.getAllSessionByUserId(id);
  }

  @Post('createMessage')
  @ApiOperation({ summary: '创建消息' })
  createMessage(@Body() body: CreateMessageDto) {
    return this.chatService.createMessage(body);
  }

  @Get('getMessages/:id')
  @ApiOperation({ summary: '根据会话id获取消息' })
  getMessageBySessionId(@Param('id') id: string) {
    return this.chatService.getMessageBySessionId(id);
  }
}
